<template>
  <el-cascader
    :value="value"
    placeholder="搜索"
    :options="options"
    filterable
    @input="inputHandle"
  />
</template>

<script>
import { initData } from '@/api/data'

export default {
  props: {
    value: {
      default: null,
      type: Array,
      required: false
    }
  },
  data() {
    return {
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      empAll: [],
      options: []
    }
  },
  created() {
    this.searchEmp()
  },
  methods: {
    searchEmp() {
      // todo 下拉菜单考虑在后台返回一个简单对象
      return new Promise((resolve, reject) => {
        // 请求数据
        initData('api/employeeBase',
          { page: -1, size: 99999, sort: 'id,asc' }
        ).then(data => {
          // 获得数据
          this.empAll = data.content
          resolve(data)
          var len1 = 0
          var len2 = 0
          var flag1 = 0
          var flag2 = 0
          var flag3 = 0
          for (var t = 0; t < this.empAll.length; t++) {
            flag3 = 0
            len1 = this.options.length
            for (var i = 0; i < len1; i++) {
              flag1 = 0
              if (this.options[i].value === this.empAll[t].centerBase.departmentId.id) {
                flag1 = 1
                len2 = this.options[i].children.length
                flag2 = 0
                for (var j = 0; j < len2; j++) {
                  if (this.options[i].children[j].value === this.empAll[t].centerBase.id) {
                    flag2 = 1
                    this.options[i].children[j].children.push({ 'value': this.empAll[t].id, 'label': this.empAll[t].empName })
                    flag3 = 1
                    break
                  }
                }
                if (flag3 === 1) break
                if (flag2 === 0) {
                  var temp_children1 = [{ 'value': this.empAll[t].id, 'label': this.empAll[t].empName }]
                  this.options[i].children.push({ 'value': this.empAll[t].centerBase.id, 'label': this.empAll[t].centerBase.centerName, 'children': temp_children1 })
                  flag3 = 1
                  break
                }
              }
              if (flag3 === 1) break
            }
            if (flag1 === 0) {
              var temp_children2 = [{ 'value': this.empAll[t].id, 'label': this.empAll[t].empName }]
              temp_children1 = [{ 'value': this.empAll[t].centerBase.id, 'label': this.empAll[t].centerBase.centerName, 'children': temp_children2 }]
              this.options.push({ 'value': this.empAll[t].centerBase.departmentId.id, 'label': this.empAll[t].centerBase.departmentId.departmentName, 'children': temp_children1 })
            }
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    inputHandle(e) {
      // console.log(e)
      this.$emit('input', e)
    }

  }
}
</script>

<style scoped>

</style>
